package com.server.model;

import com.common.User;
import com.util.JDBCUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/***用户服务类：
 * 功能：实现对用户的操作类：
 * 1.加载所有已经注册的用户
 * 2.判断用户是否登录
 *
 */
public class UserService {
    Connection conn;
    PreparedStatement statement;

    public List<User> loadAllUser() throws Exception {
        List<User> users=new ArrayList<>();
        conn= JDBCUtils.getConnection();
        //从数据库中查询所有用户，并加载相应的用户信息
        String sql="select * from  sys.user";
        statement=conn.prepareStatement(sql);
        ResultSet result=statement.executeQuery();
        while (result.next()){
            User user=new User();
            user.setId(result.getString("id"));
            user.setUsername(result.getString("username"));
            user.setRealname(result.getString("realname"));
            user.setPwd(result.getString("pwd"));
            user.setSex(result.getString("sex"));
            user.setUsergrade(0);
            user.setUservalue(0);

            //添加用户信息
            System.out.println(user.toString());
            users.add(user);
        }
        return users;
    }

    //将已有数据插入状态表(在ServerProcess类中调用参数直接插入)
    public void loadServerList() throws Exception {
        List<User> list=loadAllUser();
        conn=JDBCUtils.getConnection();
        //从数据库中插入已经注册的用户
        String sql="insert ignore into sys.serverusers values(?,?,?)";
        for(User user:list){
            statement=conn.prepareStatement(sql);
            statement.setString(1,user.getId());
            statement.setString(2,user.getPwd());
            statement.setInt(3,0);
            int res=statement.executeUpdate();      //执行插入
            if(res!=1){
                System.out.println("用户已存在");
            }else{
                System.out.println("数据更新成功");
            }
        }
    }

    //每当一个用户登录，就更改一条已登录用户的状态
    public void loadLogin(String id) throws Exception {
        conn=JDBCUtils.getConnection();
        List<User> users=loadAllUser();
        for(User user:users){
            if(user.getId().equals(id)){
                String sql="update sys.serverusers set isLogin=? where id=?";
                statement=conn.prepareStatement(sql);
                statement.setInt(1,1);
                statement.setString(2,id);
                //刷新结果
                int res=statement.executeUpdate();  //执行更新
                if(res!=1){
                    System.out.println("数据更新失败");
                }else{
                    System.out.println("数据更新成功");
                }
            }
        }
    }

    //当用户下线时，更改登录信息
    public void loadExit(String id) throws Exception {
        conn=JDBCUtils.getConnection();
        List<User> users=loadAllUser();
        for(User user:users){
            if(user.getId().equals(id)){
                String sql="update sys.serverusers set isLogin=? where id=?";
                statement=conn.prepareStatement(sql);
                statement.setInt(1,0);
                statement.setString(2,id);
                //刷新结果
                int res=statement.executeUpdate();  //执行更新
                if(res!=1){
                    System.out.println("数据更新失败");
                }else{
                    System.out.println("数据更新成功");
                }
            }
        }
    }

    //返回一个已登录用户的列表(刷新已登录用户表，即查询用户状态为1的用户信息)
    public List<User> loginUser() throws Exception {
        List<User> users=new ArrayList<>();
        conn=JDBCUtils.getConnection();
        String sql="select serverusers.id, username, sex " +
                "from " +
                "(sys.serverusers inner join sys.user on serverusers.id=user.id) " +
                "where isLogin= ?";
        statement=conn.prepareStatement(sql);
        statement.setInt(1,1);
        ResultSet result=statement.executeQuery();
        while (result.next()){
            User user=new User();
            user.setId(result.getString("id"));
            user.setUsername(result.getString("username"));
            user.setSex(result.getString("sex"));
            users.add(user);
        }
        return users;
    }
}
